"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

require("core-js/modules/es.array-buffer.constructor");

require("core-js/modules/es.array-buffer.slice");

require("core-js/modules/es.function.name");

require("core-js/modules/es.object.to-string");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _logger = _interopRequireDefault(require("./utils/logger.js"));

var _loader = require("./loader.js");

var _exception = require("./utils/exception.js");

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

// For FLV over WebSocket live stream
var WebSocketLoader = /*#__PURE__*/function (_BaseLoader) {
  (0, _inherits2.default)(WebSocketLoader, _BaseLoader);

  var _super = _createSuper(WebSocketLoader);

  (0, _createClass2.default)(WebSocketLoader, null, [{
    key: "isSupported",
    value: function isSupported() {
      try {
        return typeof self.WebSocket !== 'undefined';
      } catch (e) {
        return false;
      }
    }
  }]);

  function WebSocketLoader() {
    var _this;

    (0, _classCallCheck2.default)(this, WebSocketLoader);
    _this = _super.call(this, 'websocket-loader');
    _this.TAG = 'WebSocketLoader';
    _this._needStash = true;
    _this._ws = null;
    _this._requestAbort = false;
    _this._receivedLength = 0;
    return _this;
  }

  (0, _createClass2.default)(WebSocketLoader, [{
    key: "destroy",
    value: function destroy() {
      if (this._ws) {
        this.abort();
      }

      (0, _get2.default)((0, _getPrototypeOf2.default)(WebSocketLoader.prototype), "destroy", this).call(this);
    }
  }, {
    key: "open",
    value: function open(dataSource) {
      try {
        var ws = this._ws = new self.WebSocket(dataSource.url);
        ws.binaryType = 'arraybuffer';
        ws.onopen = this._onWebSocketOpen.bind(this);
        ws.onclose = this._onWebSocketClose.bind(this);
        ws.onmessage = this._onWebSocketMessage.bind(this);
        ws.onerror = this._onWebSocketError.bind(this);
        this._status = _loader.LoaderStatus.kConnecting;
      } catch (e) {
        this._status = _loader.LoaderStatus.kError;
        var info = {
          code: e.code,
          msg: e.message
        };

        if (this._onError) {
          this._onError(_loader.LoaderErrors.EXCEPTION, info);
        } else {
          throw new _exception.RuntimeException(info.msg);
        }
      }
    }
  }, {
    key: "abort",
    value: function abort() {
      var ws = this._ws;

      if (ws && (ws.readyState === 0 || ws.readyState === 1)) {
        // CONNECTING || OPEN
        this._requestAbort = true;
        ws.close();
      }

      this._ws = null;
      this._status = _loader.LoaderStatus.kComplete;
    }
  }, {
    key: "_onWebSocketOpen",
    value: function _onWebSocketOpen(e) {
      this._status = _loader.LoaderStatus.kBuffering;
    }
  }, {
    key: "_onWebSocketClose",
    value: function _onWebSocketClose(e) {
      if (this._requestAbort === true) {
        this._requestAbort = false;
        return;
      }

      this._status = _loader.LoaderStatus.kComplete;

      if (this._onComplete) {
        this._onComplete(0, this._receivedLength - 1);
      }
    }
  }, {
    key: "_onWebSocketMessage",
    value: function _onWebSocketMessage(e) {
      var _this2 = this;

      if (e.data instanceof ArrayBuffer) {
        this._dispatchArrayBuffer(e.data);
      } else if (e.data instanceof Blob) {
        var reader = new FileReader();

        reader.onload = function () {
          _this2._dispatchArrayBuffer(reader.result);
        };

        reader.readAsArrayBuffer(e.data);
      } else {
        this._status = _loader.LoaderStatus.kError;
        var info = {
          code: -1,
          msg: 'Unsupported WebSocket message type: ' + e.data.constructor.name
        };

        if (this._onError) {
          this._onError(_loader.LoaderErrors.EXCEPTION, info);
        } else {
          throw new _exception.RuntimeException(info.msg);
        }
      }
    }
  }, {
    key: "_dispatchArrayBuffer",
    value: function _dispatchArrayBuffer(arraybuffer) {
      var chunk = arraybuffer;
      var byteStart = this._receivedLength;
      this._receivedLength += chunk.byteLength;

      if (this._onDataArrival) {
        this._onDataArrival(chunk, byteStart, this._receivedLength);
      }
    }
  }, {
    key: "_onWebSocketError",
    value: function _onWebSocketError(e) {
      this._status = _loader.LoaderStatus.kError;
      var info = {
        code: e.code,
        msg: e.message
      };

      if (this._onError) {
        this._onError(_loader.LoaderErrors.EXCEPTION, info);
      } else {
        throw new _exception.RuntimeException(info.msg);
      }
    }
  }]);
  return WebSocketLoader;
}(_loader.BaseLoader);

var _default = WebSocketLoader;
exports.default = _default;